package org.example.Demo01;

import org.example.Join.JDBC_Join;
import org.example.pojo.Brand;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLData;
import java.util.ArrayList;
import java.util.List;

public class JDBC_Select {
    public JDBC_Select() {

    }

    //创建一个JDBC_Join对象，用于数据库的连接
    private JDBC_Join jdbcJoin = new JDBC_Join();
    public List<Brand> select() throws Exception {
        //用来存储查询出来的对象
        List<Brand> arr = new ArrayList<>();

        Connection conn = jdbcJoin.join();

        String sql = "select * from tb_brand";
        PreparedStatement prpstmt = conn.prepareStatement(sql);

        ResultSet res = prpstmt.executeQuery();

        while (res.next()){
            Brand brand = new Brand();
            //那查询的结果赋值给brand对象
            brand.setId(res.getInt(1));
            brand.setBrandName(res.getString(2));
            brand.setCompanyName(res.getString(3));
            brand.setOrdered(res.getInt(4));
            brand.setDescription(res.getString(5));
            brand.setStatus(res.getInt(6));
            arr.add(brand);
        }
        //返回查询封装好的结果
        return arr;
    }
}
